package com.heima.dao.provider;

import com.heima.pojo.QueryVO;
import com.heima.pojo.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.jdbc.SQL;

public class UserDaoProvider {
    public String selectByCondition(QueryVO queryVO){
        return new SQL(){{
            SELECT("*");
            FROM("t_user");
            if (queryVO.getUser().getUsername() != null){
                WHERE("username like concat('%',#{user.username},'%')");
            }
            if (queryVO.getUser().getSex() != null){
                AND();
                WHERE("sex = #{user.sex}");
            }
            if (queryVO.getStartIndex() != null && queryVO.getPageSize() != null){
               LIMIT("#{startIndex},#{pageSize}");
            }
        }
        }.toString();
    }
    public String update(User user){
        return new SQL(){{
            UPDATE("t_user");
            if (user.getUsername() != null){
                SET("username = #{username}");
            }
            if (user.getSex() != null){
                SET("sex = #{sex}");
            }
            if (user.getBirthday() != null){
                SET("birthday = #{birthday}");
            }
            if (user.getAddress() != null){
                SET("address = #{address}");
            }
            WHERE("id = #{id}");
        }}.toString();
    }
    public String delete(Integer[] ids){
        return new SQL(){{
            DELETE_FROM("t_user");
            for (Integer id : ids) {
                OR();
                WHERE("id = "+id);
            }
        }}.toString();
    }

}
